<template>
  <div class="login_container">
    <div class="login_box">
      <!-- logo区域 -->
      <div class="login_logo">
        <img src="../assets/logo.png" alt="">
      </div>

      <!-- 表单区域 -->
      <el-form ref="loginormRef" class="login_form" :model="loginform" :rules="rules">
        <!-- 用户名输入框 -->
        <el-form-item prop="username">
          <el-input v-model="loginform.username" prefix-icon="iconfont icon-user" placeholder="请输入账号"></el-input>
        </el-form-item>

        <!-- 密码输入框 -->
        <el-form-item prop="password">
          <el-input v-model="loginform.password" show-password prefix-icon="iconfont icon-3702mima" type="password"
            placeholder="请输入密码"></el-input>
        </el-form-item>

        <!-- 按钮 -->
        <el-form-item class="btn">

          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetloginform">重置</el-button>
          
        </el-form-item>
      </el-form>

    </div>

  </div>
</template>

<script>
  export default {
    data() {
      return {
        // 登录表单绑定对象
        loginform: {
          username: 'admin',
          password: '123456'
        },
        //登录规则验证对象
        rules: {
          username: [{
              required: true,
              message: '请输入用户名',
              trigger: 'blur'
            },
            {
              min: 3,
              max: 5,
              message: '长度在 3 到 5 个字符',
              trigger: 'blur'
            }
          ],
          password: [{
              required: true,
              message: '请输入登录密码',
              trigger: 'blur'
            },
            {
              min: 6,
              max: 10,
              message: '长度在6到10个字符',
              trigger: 'blur'
            }
          ]
        }
      }
    },
    methods: {
      // 重置表单
      resetloginform() {
        this.$refs.loginormRef.resetFields()
      },
      // 登录
      login() {
        this.$refs.loginormRef.validate(async valid => {
          if (!valid) return;
          const {
            data: res
          } = await this.$http.post("login", this.loginform);
          if (res.meta.status !== 200) return this.$message.error("登录失败");
          this.$message.success("登陆成功");
          // 将登陆成功的token保存到客户端的sessionStorge;
          window.sessionStorage.setItem("token", res.data.token);
          // 并且将导航跳转到后台主页,主页地址是home
          this.$router.push("/home");


        })
      }
    }
  }
</script>


<style lang="less" scoped>
  .login_container {
    background-color: #4d88c3;
    height: 100%;

  }

  .login_form {
    padding-top: 18px;
    padding-left: 10%;
    padding-right: 10%;
  }

  .login_box {
    height: 350px;
    width: 400px;
    background-color: #DCDCDC;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 20%;

    .login_logo {
      height: 100px;
      width: 100px;
      margin-left: 35%;

      img {
        width: 100%;
        height: 100%;
      }
    }

    .btn {
      display: block;
      float: right;
      margin-top: 15px;
      margin-right: 10%;
    }
  }
</style>
